{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import datetime\n",
    "import numpy as np \n",
    "import statsmodels.formula.api as sml\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import scipy.stats as scs\n",
    "import matplotlib.mlab as mlab\n",
    "try:\n",
    "    import QUANTAXIS as QA\n",
    "    from QUANTAXIS.QAIndicator.talib_numpy import *\n",
    "except:\n",
    "    print('PLEASE run \"pip install QUANTAXIS\" to call these modules')\n",
    "    pass\n",
    "\n",
    "import statsmodels.api as sm\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pre_rsrs_data_func(data, N=18, M=252):\n",
    "    \"\"\"\n",
    "    准备数据\n",
    "    \"\"\"\n",
    "    highs = data.high.values\n",
    "    lows = data.low.values\n",
    "    start_t = datetime.datetime.now()\n",
    "    print(start_t)\n",
    "\n",
    "    # 斜率\n",
    "    data['beta'] = 0\n",
    "    data['R2'] = 0\n",
    "    beta_rsquared = np.zeros((len(data), 2),)\n",
    "\n",
    "    for i in range(len(highs))[N:]:\n",
    "        data_high = highs[i - N:i]\n",
    "        data_low = lows[i - N:i]\n",
    "        X = sm.add_constant(data_low)\n",
    "        model = sm.OLS(data_high,X)\n",
    "        results = model.fit()\n",
    "\n",
    "        # beta = low\n",
    "        if (len(results.params) > 1):\n",
    "            beta_rsquared[i, 0] = results.params[1]\n",
    "        else:\n",
    "            beta_rsquared[i, 0] = results.params[0]\n",
    "        beta_rsquared[i, 1] = results.rsquared\n",
    "\n",
    "    data[['beta', 'R2']] = beta_rsquared\n",
    "\n",
    "    # 日收益率\n",
    "    data['ret'] = data.close.pct_change(1)\n",
    "    \n",
    "    # 标准分\n",
    "    data['beta_norm'] = data['beta'].rolling(M).apply(lambda x:scs.zscore(x.values)[-1])\n",
    "\n",
    "    beta = data.columns.get_loc('beta')\n",
    "    beta_norm = data.columns.get_loc('beta_norm')\n",
    "    data.iloc[:min(M, len(highs)), beta_norm] = scs.zscore(data.iloc[:min(M, len(highs)), beta].values)\n",
    "    data['RSRS_R2'] = data.beta_norm * data.R2\n",
    "    data = data.fillna(0)\n",
    "    \n",
    "    # 右偏标准分\n",
    "    data['beta_right'] = data.RSRS_R2 * data.beta\n",
    "\n",
    "    end_t = datetime.datetime.now()\n",
    "    print(end_t, 'spent:{}'.format((end_t - start_t)))\n",
    "    return data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pre_rsrs_data_v1_func(data, N=18, M=252):\n",
    "    \"\"\"\n",
    "    准备数据\n",
    "    \"\"\"\n",
    "    highs = data.high\n",
    "    start_t = datetime.datetime.now()\n",
    "    print(start_t)\n",
    "\n",
    "    # 斜率\n",
    "    data['beta'] = 0\n",
    "    data['R2'] = 0\n",
    "    beta_rsquared = np.zeros((len(data), 2),)\n",
    "\n",
    "    for i in range(N - 1, len(highs) - 1):\n",
    "    #for i in range(len(highs))[N:]:\n",
    "        df_ne = data.iloc[i - N + 1:i + 1, :]\n",
    "        model = sml.ols(formula='high~low', data = df_ne)\n",
    "        result = model.fit()\n",
    "\n",
    "        # beta = low\n",
    "        beta_rsquared[i + 1, 0] = result.params[1]\n",
    "        beta_rsquared[i + 1, 1] = result.rsquared\n",
    "\n",
    "    data[['beta', 'R2']] = beta_rsquared\n",
    "\n",
    "    # 日收益率\n",
    "    data['ret'] = data.close.pct_change(1)\n",
    "    \n",
    "    # 标准分\n",
    "    data['beta_norm'] = (data['beta'] - data.beta.rolling(M).mean().shift(1)) / data.beta.rolling(M).std().shift(1)\n",
    "\n",
    "    beta_norm = data.columns.get_loc('beta_norm')\n",
    "    beta = data.columns.get_loc('beta')\n",
    "    for i in range(min(M, len(highs))):\n",
    "        data.iat[i, beta_norm] = (data.iat[i, beta] - data.iloc[:i - 1, beta].mean()) / data.iloc[:i - 1, beta].std() if (data.iloc[:i - 1, beta].std() != 0) else np.nan\n",
    "\n",
    "    data.iat[2, beta_norm] = 0\n",
    "    data['RSRS_R2'] = data.beta_norm * data.R2\n",
    "    data = data.fillna(0)\n",
    "    \n",
    "    # 右偏标准分\n",
    "    data['beta_right'] = data.RSRS_R2 * data.beta\n",
    "    end_t = datetime.datetime.now()\n",
    "    print(end_t, 'spent:{}'.format((end_t - start_t)))\n",
    "    return data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pre_rsrs_data_v2_func(data, N=18, M=252):\n",
    "    \"\"\"\n",
    "    准备数据\n",
    "    \"\"\"\n",
    "    highs = data.high\n",
    "\n",
    "    # 斜率\n",
    "    data['beta'] = 0\n",
    "    data['R2'] = 0\n",
    "    beta_rsquared = np.zeros((len(data), 2),)\n",
    "\n",
    "    for i in range(N - 1, len(highs) - 1):\n",
    "    #for i in range(len(highs))[N:]:\n",
    "        df_ne = data.iloc[i - N + 1:i + 1, :]\n",
    "        model = sml.ols(formula='high~low',data = df_ne)\n",
    "        result = model.fit()\n",
    "\n",
    "        # beta = low\n",
    "        beta_rsquared[i + 1, 0] = result.params[1]\n",
    "        beta_rsquared[i + 1, 1] = result.rsquared\n",
    "\n",
    "    data[['beta', 'R2']] = beta_rsquared\n",
    "\n",
    "    # 日收益率\n",
    "    data['ret'] = data.close.pct_change(1)\n",
    "    \n",
    "    # 标准分\n",
    "    #data['beta_norm'] = (data['beta'] - data.beta.rolling(M).mean().shift(1))\n",
    "    #/ data.beta.rolling(M).std().shift(1)\n",
    "    data['beta_norm'] = data['beta'].rolling(M).apply(lambda x:scs.zscore(x.values)[-1])\n",
    "\n",
    "    beta = data.columns.get_loc('beta')\n",
    "    beta_norm = data.columns.get_loc('beta_norm')\n",
    "    data.iloc[:min(M, len(highs)), beta_norm] = scs.zscore(data.iloc[:min(M, len(highs)), beta].values)\n",
    "    #beta_norm = data.columns.get_loc('beta_norm')\n",
    "    #beta = data.columns.get_loc('beta')\n",
    "    #for i in range(min(M, len(highs))):\n",
    "    # data.iat[i, beta_norm] = (data.iat[i, beta] - data.iloc[:i - 1,\n",
    "    # beta].mean()) / data.iloc[:i - 1, beta].std() if (data.iloc[:i - 1,\n",
    "    # beta].std() != 0) else np.nan\n",
    "\n",
    "    #data.iat[2, beta_norm] = 0\n",
    "    data['RSRS_R2'] = data.beta_norm * data.R2\n",
    "    data = data.fillna(0)\n",
    "    \n",
    "    # 右偏标准分\n",
    "    data['beta_right'] = data.RSRS_R2 * data.beta\n",
    "\n",
    "    return data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def RSRS1(price, S1=1.0, S2=0.8):\n",
    "    \"\"\"\n",
    "    斜率指标交易策略标准分策略\n",
    "    \"\"\"\n",
    "    data = price.copy()\n",
    "    data['flag'] = 0 # 买卖标记\n",
    "    data['position'] = 0 # 持仓标记\n",
    "\n",
    "    beta = data.columns.get_loc('beta')\n",
    "    flag = data.columns.get_loc('flag')\n",
    "    position_col = data.columns.get_loc('position')\n",
    "\n",
    "    position = 0 # 是否持仓，持仓：1，不持仓：0\n",
    "    for i in range(1,data.shape[0] - 1):\n",
    "        # 开仓\n",
    "        if data.iat[i, beta] > S1 and position == 0:\n",
    "            data.iat[i, flag] = 1\n",
    "            data.iat[i + 1, position_col] = 1\n",
    "            position = 1\n",
    "\n",
    "        # 平仓\n",
    "        elif data.iat[i, beta] < S2 and position == 1: \n",
    "            data.iat[i, flag] = -1\n",
    "            data.iat[i + 1, position_col] = 0     \n",
    "            position = 0\n",
    "        \n",
    "        # 保持\n",
    "        else:\n",
    "            data.iat[i + 1, position_col] = data.iat[i, position_col]\n",
    "        \n",
    "    data['nav'] = (1 + data.close.pct_change(1).fillna(0) * data.position).cumprod() \n",
    "        \n",
    "    return(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def RSRS2(price,\n",
    "          S=0.7):\n",
    "    \"\"\"\n",
    "    标准分策略\n",
    "    \"\"\"\n",
    "    data = price.copy()\n",
    "    data['flag'] = 0 # 买卖标记\n",
    "    data['position'] = 0 # 持仓标记\n",
    "\n",
    "    beta_norm = data.columns.get_loc('beta_norm')\n",
    "    flag = data.columns.get_loc('flag')\n",
    "    position_col = data.columns.get_loc('position')\n",
    "\n",
    "    position = 0 # 是否持仓，持仓：1，不持仓：0\n",
    "    for i in range(1,data.shape[0] - 1):\n",
    "        # 开仓\n",
    "        if data.iat[i, beta_norm] > S and position == 0:\n",
    "            data.iat[i, flag] = 1\n",
    "            data.iat[i + 1, position_col] = 1\n",
    "            position = 1\n",
    "        # 平仓\n",
    "        elif data.iat[i, beta_norm] < -S and position == 1: \n",
    "            data.iat[i, flag] = -1\n",
    "            data.iat[i + 1, position_col] = 0     \n",
    "            position = 0\n",
    "        \n",
    "        # 保持\n",
    "        else:\n",
    "            data.iat[i + 1, position_col] = data.iat[i, position_col]\n",
    "        \n",
    "    data['nav'] = (1 + data.close.pct_change(1).fillna(0) * data.position).cumprod() \n",
    "           \n",
    "    return(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def RSRS3(HS300,S=0.7):\n",
    "    \"\"\"\n",
    "    修正标准分策略\n",
    "    \"\"\"\n",
    "    data = HS300.copy()\n",
    "    data['flag'] = 0 # 买卖标记\n",
    "    data['position'] = 0 # 持仓标记\n",
    "\n",
    "    RSRS_R2 = data.columns.get_loc('RSRS_R2')\n",
    "    flag = data.columns.get_loc('flag')\n",
    "    position_col = data.columns.get_loc('position')\n",
    "\n",
    "    position = 0 # 是否持仓，持仓：1，不持仓：0\n",
    "    for i in range(1,data.shape[0] - 1):\n",
    "        # 开仓\n",
    "        if data.iat[i, RSRS_R2] > S and position == 0:\n",
    "            data.iat[i, flag] = 1\n",
    "            data.iat[i + 1, position_col] = 1\n",
    "            position = 1\n",
    "        # 平仓\n",
    "        elif data.iat[i, RSRS_R2] < -S and position == 1: \n",
    "            data.iat[i, flag] = -1\n",
    "            data.iat[i + 1, position_col] = 0      \n",
    "            position = 0\n",
    "        # 保持\n",
    "        else:\n",
    "            data.iat[i + 1, position_col] = data.iat[i, position_col]\n",
    "        \n",
    "    data['nav'] = (1 + data.close.pct_change(1).fillna(0) * data.position).cumprod() \n",
    "           \n",
    "    return(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def RSRS4(price,\n",
    "          S=0.7):\n",
    "    \"\"\"\n",
    "    #右偏标准分策略\n",
    "    \"\"\"\n",
    "    data = price.copy()\n",
    "    data['flag'] = 0 # 买卖标记\n",
    "    data['position'] = 0 # 持仓标记\n",
    "\n",
    "    beta_right = data.columns.get_loc('beta_right')\n",
    "    flag = data.columns.get_loc('flag')\n",
    "    position_col = data.columns.get_loc('position')\n",
    "\n",
    "    position = 0 # 是否持仓，持仓：1，不持仓：0\n",
    "    for i in range(1,data.shape[0] - 1):\n",
    "        \n",
    "        # 开仓\n",
    "        if data.iat[i, beta_right] > S and position == 0:\n",
    "            data.iat[i, flag] = 1\n",
    "            data.iat[i + 1, position_col] = 1\n",
    "            position = 1\n",
    "        # 平仓\n",
    "        elif data.iat[i, beta_right] < -S and position == 1: \n",
    "            data.iat[i, flag] = -1\n",
    "            data.iat[i + 1, position_col] = 0     \n",
    "            position = 0\n",
    "        \n",
    "        # 保持\n",
    "        else:\n",
    "            data.iat[i + 1, position_col] = data.iat[i, position_col]     \n",
    "        \n",
    "    data['nav'] = (1 + data.close.pct_change(1).fillna(0) * data.position).cumprod() \n",
    "           \n",
    "    return(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "codelist = ['600756']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_names = QA.QA_fetch_stock_name(codelist)\n",
    "codename = [stock_names.at[code] for code in codelist]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_day = QA.QA_fetch_stock_day_adv(codelist,\n",
    "                                        '2019-01-01',\n",
    "                                        '{}'.format(datetime.date.today(),)).to_qfq()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "indices_rsrsT = data_day.add_func(pre_rsrs_data_v1_func)\n",
    "resultT = RSRS1(indices_rsrsT)\n",
    "num = resultT.flag.abs().sum() / 2\n",
    "nav = resultT.nav[resultT.shape[0] - 1]\n",
    "print('RSRS1_T 交易次数 = ',num)\n",
    "print('策略净值为= ',nav)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resultT2 = RSRS2(indices_rsrsT)\n",
    "num = resultT2.flag.abs().sum() / 2\n",
    "nav = resultT2.nav[resultT2.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS2_T 交易次数 = ',num)\n",
    "print('策略净值为= ',nav)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resultT3 = RSRS3(indices_rsrsT)\n",
    "num = resultT3.flag.abs().sum() / 2\n",
    "nav = resultT3.nav[resultT.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS3_T 交易次数 = ',num)\n",
    "print('策略净值为= ',nav)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resultT4 = RSRS4(indices_rsrsT)\n",
    "num = resultT4.flag.abs().sum() / 2\n",
    "nav = resultT4.nav[resultT.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS4_T 交易次数 = ',num)\n",
    "print('策略净值为= ',nav)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-06-12 13:47:45.707458\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/zhangjx/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:45: FutureWarning: Currently, 'apply' passes the values as ndarrays to the applied function. In the future, this will change to passing it as Series objects. You need to specify 'raw=True' to keep the current behaviour, and you can pass 'raw=False' to silence this warning\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-06-12 13:47:45.873154\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'numpy.ndarray' object has no attribute 'values'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, *args, **kwargs)\u001b[0m\n\u001b[1;32m    917\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 918\u001b[0;31m                 \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_python_apply_general\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    919\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36m_python_apply_general\u001b[0;34m(self, f)\u001b[0m\n\u001b[1;32m    935\u001b[0m         keys, values, mutated = self.grouper.apply(f, self._selected_obj,\n\u001b[0;32m--> 936\u001b[0;31m                                                    self.axis)\n\u001b[0m\u001b[1;32m    937\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, f, data, axis)\u001b[0m\n\u001b[1;32m   2272\u001b[0m             \u001b[0mgroup_axes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_get_axes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2273\u001b[0;31m             \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2274\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_is_indexed_like\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgroup_axes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-2-d7f566926e2c>\u001b[0m in \u001b[0;36mpre_rsrs_data_func\u001b[0;34m(data, N, M)\u001b[0m\n\u001b[1;32m     44\u001b[0m     \u001b[0;31m# 标准分\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 45\u001b[0;31m     \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'beta_norm'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'beta'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrolling\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mscs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzscore\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     46\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, raw, args, kwargs)\u001b[0m\n\u001b[1;32m   1579\u001b[0m         return super(Rolling, self).apply(\n\u001b[0;32m-> 1580\u001b[0;31m             func, raw=raw, args=args, kwargs=kwargs)\n\u001b[0m\u001b[1;32m   1581\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, raw, args, kwargs)\u001b[0m\n\u001b[1;32m   1002\u001b[0m         return self._apply(f, func, args=args, kwargs=kwargs,\n\u001b[0;32m-> 1003\u001b[0;31m                            center=False, raw=raw)\n\u001b[0m\u001b[1;32m   1004\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36m_apply\u001b[0;34m(self, func, name, window, center, check_minp, **kwargs)\u001b[0m\n\u001b[1;32m    882\u001b[0m                 \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 883\u001b[0;31m                     \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcalc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    884\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36mcalc\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m    876\u001b[0m                     return func(x, window, min_periods=self.min_periods,\n\u001b[0;32m--> 877\u001b[0;31m                                 closed=self.closed)\n\u001b[0m\u001b[1;32m    878\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36mf\u001b[0;34m(arg, window, min_periods, closed)\u001b[0m\n\u001b[1;32m    999\u001b[0m                 \u001b[0marg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwindow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mminp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindexi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1000\u001b[0;31m                 closed, offset, func, raw, args, kwargs)\n\u001b[0m\u001b[1;32m   1001\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/window.pyx\u001b[0m in \u001b[0;36mpandas._libs.window.roll_generic\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32m<ipython-input-2-d7f566926e2c>\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m     44\u001b[0m     \u001b[0;31m# 标准分\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 45\u001b[0;31m     \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'beta_norm'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'beta'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrolling\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mscs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzscore\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     46\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'values'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-26-f272c5c3134d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mindices_rsrs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata_day\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpre_rsrs_data_func\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/QUANTAXIS/QAData/base_datastruct.py\u001b[0m in \u001b[0;36madd_func\u001b[0;34m(self, func, *arg, **kwargs)\u001b[0m\n\u001b[1;32m   1043\u001b[0m         \"\"\"\n\u001b[1;32m   1044\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1045\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroupby\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlevel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msort\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1046\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1047\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0madd_funcx\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, *args, **kwargs)\u001b[0m\n\u001b[1;32m    928\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    929\u001b[0m                 \u001b[0;32mwith\u001b[0m \u001b[0m_group_selection_context\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 930\u001b[0;31m                     \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_python_apply_general\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    931\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    932\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36m_python_apply_general\u001b[0;34m(self, f)\u001b[0m\n\u001b[1;32m    934\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_python_apply_general\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    935\u001b[0m         keys, values, mutated = self.grouper.apply(f, self._selected_obj,\n\u001b[0;32m--> 936\u001b[0;31m                                                    self.axis)\n\u001b[0m\u001b[1;32m    937\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    938\u001b[0m         return self._wrap_applied_output(\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, f, data, axis)\u001b[0m\n\u001b[1;32m   2271\u001b[0m             \u001b[0;31m# group might be modified\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2272\u001b[0m             \u001b[0mgroup_axes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_get_axes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2273\u001b[0;31m             \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2274\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_is_indexed_like\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgroup_axes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2275\u001b[0m                 \u001b[0mmutated\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-2-d7f566926e2c>\u001b[0m in \u001b[0;36mpre_rsrs_data_func\u001b[0;34m(data, N, M)\u001b[0m\n\u001b[1;32m     43\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     44\u001b[0m     \u001b[0;31m# 标准分\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 45\u001b[0;31m     \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'beta_norm'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'beta'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrolling\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mscs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzscore\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     46\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     47\u001b[0m     \u001b[0mbeta\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'beta'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, raw, args, kwargs)\u001b[0m\n\u001b[1;32m   1578\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mraw\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1579\u001b[0m         return super(Rolling, self).apply(\n\u001b[0;32m-> 1580\u001b[0;31m             func, raw=raw, args=args, kwargs=kwargs)\n\u001b[0m\u001b[1;32m   1581\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1582\u001b[0m     \u001b[0;34m@\u001b[0m\u001b[0mSubstitution\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'rolling'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, raw, args, kwargs)\u001b[0m\n\u001b[1;32m   1001\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1002\u001b[0m         return self._apply(f, func, args=args, kwargs=kwargs,\n\u001b[0;32m-> 1003\u001b[0;31m                            center=False, raw=raw)\n\u001b[0m\u001b[1;32m   1004\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1005\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36m_apply\u001b[0;34m(self, func, name, window, center, check_minp, **kwargs)\u001b[0m\n\u001b[1;32m    881\u001b[0m                     \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply_along_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcalc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    882\u001b[0m                 \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 883\u001b[0;31m                     \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcalc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    884\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    885\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mcenter\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36mcalc\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m    875\u001b[0m                 \u001b[0;32mdef\u001b[0m \u001b[0mcalc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    876\u001b[0m                     return func(x, window, min_periods=self.min_periods,\n\u001b[0;32m--> 877\u001b[0;31m                                 closed=self.closed)\n\u001b[0m\u001b[1;32m    878\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    879\u001b[0m             \u001b[0;32mwith\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0merrstate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mall\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'ignore'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/window.py\u001b[0m in \u001b[0;36mf\u001b[0;34m(arg, window, min_periods, closed)\u001b[0m\n\u001b[1;32m    998\u001b[0m             return _window.roll_generic(\n\u001b[1;32m    999\u001b[0m                 \u001b[0marg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwindow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mminp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindexi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1000\u001b[0;31m                 closed, offset, func, raw, args, kwargs)\n\u001b[0m\u001b[1;32m   1001\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1002\u001b[0m         return self._apply(f, func, args=args, kwargs=kwargs,\n",
      "\u001b[0;32mpandas/_libs/window.pyx\u001b[0m in \u001b[0;36mpandas._libs.window.roll_generic\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32m<ipython-input-2-d7f566926e2c>\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m     43\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     44\u001b[0m     \u001b[0;31m# 标准分\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 45\u001b[0;31m     \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'beta_norm'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'beta'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrolling\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mscs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzscore\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     46\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     47\u001b[0m     \u001b[0mbeta\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'beta'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'values'"
     ]
    }
   ],
   "source": [
    "indices_rsrs = data_day.add_func(pre_rsrs_data_func)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "indices_rsrs = data_day.add_func(pre_rsrs_data_func)\n",
    "result = RSRS1(indices_rsrs)\n",
    "print(indices_rsrs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num = result.flag.abs().sum() / 2\n",
    "nav = result.nav[result.shape[0] - 1]\n",
    "print('RSRS1 交易次数 = ',num)\n",
    "print('策略净值为= ',nav)\n",
    "print(result[['close', 'ret' ,'beta', 'R2', 'beta_norm', 'RSRS_R2', 'flag', 'position', 'nav']].tail(50))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result2 = RSRS2(indices_rsrs)\n",
    "num = result2.flag.abs().sum() / 2\n",
    "nav = result2.nav[result.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS2 交易次数 = ',num)\n",
    "print('策略净值为= ',nav)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result3 = RSRS3(indices_rsrs)\n",
    "num = result3.flag.abs().sum() / 2\n",
    "nav = result3.nav[result.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS3 交易次数 = ',num)\n",
    "print('策略净值为= ',nav)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result4 = RSRS4(indices_rsrs)\n",
    "num = result4.flag.abs().sum() / 2\n",
    "nav = result4.nav[result.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS4 交易次数 = ',num)\n",
    "print('策略净值为= ',nav)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xticklabel = result.index.get_level_values(level=0).to_series().apply(lambda x: x.strftime(\"%Y-%m-%d\")[2:16])\n",
    "\n",
    "plt.figure(figsize=(15,3))\n",
    "fig = plt.axes()\n",
    "plt.plot(np.arange(result.shape[0]), result.nav,label = 'RSRS1',linewidth = 2)\n",
    "plt.plot(np.arange(result.shape[0]), result2.nav,label = 'RSRS2',linewidth = 2)\n",
    "plt.plot(np.arange(result.shape[0]), result3.nav,label = 'RSRS3',linewidth = 2)\n",
    "plt.plot(np.arange(result.shape[0]), result4.nav,label = 'RSRS4',linewidth = 2)\n",
    "plt.plot(np.arange(result.shape[0]), indices_rsrs.close / indices_rsrs.close[0], label = codelist[0], linewidth = 2)\n",
    "\n",
    "fig.set_xticks(range(0, len(xticklabel), \n",
    "                     round(len(xticklabel) / 12)))\n",
    "fig.set_xticklabels(xticklabel[::round(len(xticklabel) / 12)],\n",
    "                    rotation = 45)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
